#!/usr/bin/python
#coding=utf-8

import sys
import getopt
import logging

from curl import login
from common import usage, parking, getUserData, showUserInfo

logging.basicConfig(level=logging.DEBUG)
#logging.basicConfig(level=logging.INFO)

def main(argv):
    try:
        opts, args = getopt.getopt(argv, "u:p:htl");
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    opt = dict(opts)

    if opt.has_key('-u') and opt.has_key('-p'):
        user = opt['-u']
        passwd = opt['-p']
        cookies = login(user, passwd)
    else:
        print "Error: set your username & password plz"
        usage()
        sys.exit(2)

    if opt.has_key('-h'):
        usage()
        sys.exit()

    if opt.has_key('-t') or opt.has_key('-l'):
        data = getUserData(cookies)
        if data == False:
            print "Error: set your username & password plz"
            usage()
            sys.exit(2)

        user = data['cars']['user']
        car = data['cars']['car']
        friends = data['friends']
        verify = data['verify']
        acc = data['acc']
        showUserInfo(user, car)

    if opt.has_key('-t'):
        parking(cookies, user, car, friends, verify, acc)

if __name__ == "__main__":
    main(sys.argv[1:])
